<?php

class facebook_insightsActions extends sfActions{

    public function executeIndex(sfWebRequest $request){
        $myLib = new myLib();
        
        /* valida que el usuario este conectado */
        $myLib->validateAuthentication("Facebook");

        $hybridauth = new Hybrid_Auth($myLib->getHybridAuthConfig());
        $facebook = $hybridauth->getAdapter("Facebook");
        

        /*
        $response = $facebook->api()->api(array(
            'method' => 'fql.query',
            'query' => "select name from profile where id=me()",
          ));

        print_r( $response );

        echo "<br />";*/

        $accounts = $facebook->api()->api('/me/accounts');
        
        /*
        foreach( $accounts['data'] as $account ){
              $params = array(
                'access_token' => $account['access_token'],
                'message' => "hello word!"
              );

              // ask facebook api to post the message to the selected account
              //$facebook->api()->api( "/" . $account['id'] . "/feed", 'POST', $params );
        }
        */
        
        $this->accounts = $accounts;
    }

    public function executeAjax_search(sfWebRequest $request){
        $resultado = "";
        $account = $request->getParameter('account');
        $metric = $request->getParameter('metric');
        $end_date = $request->getParameter('end_date');
        $period = $request->getParameter('period');

        $myLib = new myLib();
        $hybridauth = new Hybrid_Auth($myLib->getHybridAuthConfig());
        $facebook = $hybridauth->getAdapter("Facebook");

        $query = "SELECT metric, value FROM insights WHERE object_id='".$account."' AND metric='".$metric."' AND end_time=end_time_date('".$end_date."') AND period=period('".$period."')";
        $resultado = $facebook->api()->api(array(
            'method' => 'fql.query',
            'query' => $query,
        ));
        
        $row = $resultado[0];

        echo $row["metric"].": ".$row["value"];
        return sfView::NONE;
    }
}
